package hot100;

public class SortedArrayToBST {
    public class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
        }

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }

    public TreeNode sortedArrayToBST(int[] nums) {
        return construct(nums,0,nums.length - 1);
    }

    private TreeNode construct(int[] nums, int leftInd, int rightInd) {
        if (leftInd > rightInd) return null;
        int midInd = (leftInd + rightInd) / 2;
        TreeNode node = new TreeNode(nums[midInd]);
        node.left = construct(nums,leftInd,midInd - 1);
        node.right = construct(nums,midInd + 1,rightInd);
        return node;
    }
}
